//
//  DrawBooleanButtonView.m
//  YiJuZhangShou2.0
//
//  Created by URANUS on 16/3/24.
//  Copyright © 2016年 Mr.X. All rights reserved.
//

#import "DrawBooleanButtonView.h"
#import "DrawPickBooleanView.h"
#import "DrawBlankBooleanView.h"
#import "Masonry.h"
@interface DrawBooleanButtonView()
@property (nonatomic,weak) id target;
@property (nonatomic, assign) SEL action;
@property (nonatomic, strong) DrawPickBooleanView *pickView;
@property (nonatomic, strong) DrawBlankBooleanView *blankView;
@end

@implementation DrawBooleanButtonView
#pragma mark Initialize
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self loadDrawBooleanButtonView];
    }
    return self;
}


#pragma mark - Overried
-(void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents{
    self.target = target;
    self.action = action;
    [super addTarget:target action:action forControlEvents:controlEvents];
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
        //获取触摸对象
         UITouch *touche = [touches anyObject];
         //获取touche的位置
         CGPoint point = [touche locationInView:self];
    
         //判断点是否在button中
         if (CGRectContainsPoint(self.bounds, point))
             {
                //执行action
                 if (self.target != nil&&self.action != nil){
                     _isSelect = !_isSelect;
                     [self loadDrawBooleanButtonView];
                     [self.target performSelector:self.action withObject:self];
                 }
           }
}

#pragma mark - Private
- (void)loadDrawBooleanButtonView{
    if (_isSelect == YES) {
        [self.blankView removeFromSuperview];
        [self addSubview:self.pickView];
        [self.pickView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
    }else{
        [self.pickView removeFromSuperview];
        [self addSubview:self.blankView];
        [self.blankView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
    }
}

#pragma mark Getter
-(DrawPickBooleanView *)pickView{

    if (_pickView == nil) {
        _pickView = [DrawPickBooleanView new];
        _pickView.borderColor = [UIColor colorWithRed:193/255 green:209/255.0 blue:230/255.0 alpha:1.0];
        _pickView.strokeColor = [UIColor colorWithRed:36/255.0 green:127/255.0 blue:209/255.0 alpha:1.0];
        _pickView.backgroundColor = [UIColor colorWithRed:236/255.0 green:236/255.0 blue:236/255.0 alpha:1.0];
    }
    return _pickView;
}

-(DrawBlankBooleanView *)blankView{

    if (_blankView == nil) {
        _blankView = [DrawBlankBooleanView new];
        _blankView.borderColor = [UIColor colorWithRed:171/255.0 green:171/255.0 blue:171/255.0 alpha:1.0];
        _blankView.backgroundColor = [UIColor colorWithRed:236/255.0 green:236/255.0 blue:236/255.0 alpha:1.0];
    }
    return _blankView;
}

#pragma mark - Setter
- (void)setIsSelect:(Boolean)isSelect{
    _isSelect = isSelect;
    [self loadDrawBooleanButtonView];
}

-(void)setPickBorderColor:(UIColor *)pickBorderColor{
    self.pickView.borderColor = pickBorderColor;
}

-(void)setPickStrokeColor:(UIColor *)pickStrokeColor{
    self.pickView.strokeColor = pickStrokeColor;
}

-(void)setPickBackgoundColor:(UIColor *)pickBackgoundColor{
    self.pickView.backgroundColor = pickBackgoundColor;
}

-(void)setBlankBorderColor:(UIColor *)blankBorderColor{
    self.blankView.borderColor = blankBorderColor;
}

-(void)setBlankBackgoundColor:(UIColor *)blankBackgoundColor{
    self.blankView.backgroundColor = blankBackgoundColor;
}

@end
